package com.aisier.architecture.base

import android.app.Application
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.multidex.MultiDex
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.Utils
import com.drake.logcat.LogCat
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.FormatStrategy
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy

/*
  * ================================================
  * 描述：基类 的app
  * 作者：zhuangzeqin
  * 时间: 2021/11/5-16:13
  * 邮箱：zzq@eeepay.cn
  * 备注:
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  */
open class BaseApp : Application() {
    companion object {
        lateinit var instance: BaseApp
        private set
    }
    override fun onCreate() {
        super.onCreate()
        instance = this
        MultiDex.install(this)
        Utils.init(this)
        //初始化Logger
        initLogger()
        ProcessLifecycleOwner.get().lifecycle.addObserver(ApplicationLifecycleObserver())
    }

     /**
      * 初始化Logger
      */
     private fun initLogger() {
         val formatStrategy: FormatStrategy = PrettyFormatStrategy.newBuilder()
             .showThreadInfo(true) // (Optional) Whether to show thread info or not. Default true
             .methodCount(0) // (Optional) How many method line to show. Default 2
             .methodOffset(500) // (Optional) Skips some method invokes in stack trace. Default 5
             //        .logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
             .tag("MyTag") // (Optional) Custom tag for each log. Default PRETTY_LOGGER
             .build()
         Logger.addLogAdapter(object : AndroidLogAdapter(formatStrategy) {
             override fun isLoggable(priority: Int, tag: String?): Boolean {
                 //true将打印日志消息，false将忽略它。这里设置Debug 模式下开始打印日志
//                return BuildConfig.DEBUG;
                 return AppUtils.isAppDebug()
             }
         })

         LogCat.config {
             defaultTag = "LogCat"
             enabled = AppUtils.isAppDebug()
//             addInterceptor(LogStoreInterceptor())
         }

//         KLog.init(true,"KLog")
     }

    private inner class ApplicationLifecycleObserver : LifecycleObserver {

        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        private fun onAppForeground() {
            // TODO: "ApplicationObserver: app moved to foreground"
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        private fun onAppBackground() {
            // TODO: "ApplicationObserver: app moved to background"
        }
    }
}